<?php

	// error codes
	define('PIXEL_METHOD_MISSING',          -1);
	define('PIXEL_ROUTE_ERROR_NO_LOAD',     -2);
    define('PIXEL_NO_CONTROLLER',           -3);
    define('PIXEL_CONTROLLER_NO_OBJECT',    -4);
    define('PIXEL_ACTION_NOT_FOUND',        -5);
	define('PIXEL_NO_TEMPLATE_FOUND',	    -6);
	define('PIXEL_TEMPLATE_ERROR_ASSIGN',   -7);
	define('PIXEL_ERROR_ASSIGNREF',         -8);
	define('PIXEL_ERROR_BEFORE_FILTER',     -9);
	define('PIXEL_ERROR_NO_MODEL',          -10);
	define('PIXEL_NO_TABLE',                -11);
	define('PIXEL_DATETIME_INCORRECT_FORMAT', -12);
	define('PIXEL_ACL_NO_CONNECTION',       -13);
	if ( !isset($GLOBALS['PIXEL']['error']) )
	{
	    $GLOBALS['PIXEL']['error'] = array(
	        PIXEL_METHOD_MISSING            => 'no method answered to requested action',
	        PIXEL_ROUTE_ERROR_NO_LOAD       => "A valid route failed to load. Processing halted",
            PIXEL_NO_CONTROLLER             => 'No controller responded to request.',
            PIXEL_CONTROLLER_NO_OBJECT      => 'Controller class could not be instantiated. Bummer',
            PIXEL_ACTION_NOT_FOUND          => 'Action not found in method',
        	PIXEL_NO_TEMPLATE_FOUND         => 'Template file not found.',
        	PIXEL_TEMPLATE_ERROR_ASSIGN     => 'Error assigning value to tpl_vars',
        	PIXEL_ERROR_ASSIGNREF           => 'Error assigning by reference to tpl_vars',
        	PIXEL_ERROR_BEFORE_FILTER       => 'An error occured running a before filter. processing halted',
        	PIXEL_ERROR_NO_MODEL            => 'The model you are trying to access is not loaded',
        	PIXEL_NO_TABLE                  => 'No table was specified in your model declaration',
        	PIXEL_DATETIME_INCORRECT_FORMAT => 'The datetime format for your field is incorrect, save not performed',
        	PIXEL_ACL_NO_CONNECTION         => 'No database connection available for ACL class.'
	    );
	}
	
    // error constants
    define('FATAL',   E_USER_ERROR);
    define('ERROR',   E_USER_WARNING);
    define('WARNING', E_USER_NOTICE);    
    
    // log all errors
    ini_set("log_errors", 1);
    ini_set("error_log", LOG."/errors.log");
    
    // debug level
    switch ( $GLOBALS['PIXEL']['DEBUG_LEVEL'] )
    {
        case 0:
            ini_set('display_errors', 0);
            error_reporting(0);
        break;
        
        case 1:
            ini_set('display_errors', 1);
            error_reporting(E_ALL ^ E_NOTICE);
        break;
              
        case 2:
            ini_set('display_errors', 1);
            error_reporting(E_ALL);
        break;
    } 
    
    // this has to be changed at some point.
	function handle_error($type, $string, $file, $line, $vars)
    {
        // error code if one is set
        if (isset($GLOBALS['PIXEL']['error'][$string])) {
			$string = $GLOBALS['PIXEL']['error'][$string];
		}
        
        // Decide which type of error it is, and handle appropriately
        switch ($type)
        {
            // Error type
            case FATAL:
                // Select debug level
                switch ( $GLOBALS['PIXEL']['DEBUG_LEVEL'] )
                {
                    default:
                    case 0:
                        echo 'There has been an error. Sorry for the inconvenience.';
                        // Stop application
                        exit;
                    case 1:
                        echo 'Error: '.$string.'<br />';
                        exit;
                    case 2:
                        echo 'Error: '.$string.' in '.$file.' on line '. $line.'<br /><pre>';
                        var_dump($vars);
                        echo "</pre>";
                        // Stop application
                        exit;
                }
            case ERROR:
                echo '<pre><b>ERROR</b> ['.$type.'] '.$string.'<br />'."</pre>";
            break;
            case WARNING:
                echo '<pre><b>WARNING</b> ['.$type.'] '.$string.'<br />'."</pre>";
            break;
        }
    }
    set_error_handler('handle_error'); 

    class PixelAppError 
    {
	
    	var $code = null;
    	var $info = array();
    	var $text = null;
    	var $backtrace = null;
    	function PixelAppError($conf = array())
    	{
    		// set public properties
    		foreach ($conf as $key => $val) {
    			$this->$key = $val;
    		}
		
    		// generate a backtrace
    		if (function_exists('debug_backtrace')) {
    			$this->backtrace = debug_backtrace();
    		}
		
    		// extended behaviors
    		$this->error();
    	}
	
    	function error(){}  
    }

?>